/*****************************************************************************
 * Copyright (C) Stacy Curl. All rights reserved.                            *
 * ------------------------------------------------------------------------- *
 * The software in this package is published under the terms of the BSD      *
 * style license a copy of which has been included with this distribution in *
 * the LICENSE.txt file.                                                     *
 *****************************************************************************/
package picounit.features.story;

import picounit.PicoUnitException;
import picounit.features.test_instantiator.InvocationArgumentHandler;
import picounit.features.test_instantiator.Invoker;
import picounit.mocker.MockControl;
import picounit.story.Event;
import picounit.story.Given;
import picounit.story.Outcome;
import picounit.story.Verify;

import java.lang.reflect.InvocationTargetException;
import java.util.LinkedList;
import java.util.List;

public class DefaultVerify implements Verify {
	private final List<Given> givens = new LinkedList<Given>();
	private final Invoker invoker;
	private final MockControl mockControl;
	private final InvocationArgumentHandler argumentHandler;

	public DefaultVerify(Invoker invoker, MockControl mockControl) {
		this.invoker = invoker;
		this.mockControl = mockControl;

		this.argumentHandler = new InvocationArgumentHandler() {
			public void preInvocation(Object[] arguments) {
				for (Object argument : arguments) {
					DefaultVerify.this.mockControl.record(argument);
				}
			}

			public void postInvocation(Object[] arguments) {
			}
		};
	}

	public Verify given(Given given) {
		applyGiven(given);

		return this;
	}

	public Verify and(Given given) {
		applyGiven(given);

		return this;
	}

	public Verify when(Event event) {
		mockControl.expectAboveWhenTheFollowingOccurs();

		fireEvent(event);

		return this;
	}

	public void then(Outcome outcome) {
		mockControl.verify();

		verifyOutcome(outcome);
	}

	public void tearDown() {
		for (Given given : givens) {
			invoke("tearDown", given);
		}

		givens.clear();
	}

	///////////////////////////////////////////////////////////////////////////////////////////////
	// Implementation methods
	///////////////////////////////////////////////////////////////////////////////////////////////

	private void applyGiven(Given given) {
		invoke("setUp", given, argumentHandler);
		
		givens.add(0, given);
	}

	private void fireEvent(Event event) {
		invoke("fire", event);
	}

	private void verifyOutcome(Outcome outcome) {
		invoke("verify", outcome);
	}

	private void invoke(String method, Object target) {
		try {
			invoker.invoke(method, target);
		}
		catch (IllegalArgumentException e) {
			throw new PicoUnitException(e);
		}
		catch (IllegalAccessException e) {
			throw new PicoUnitException(e);
		}
		catch (InvocationTargetException e) {
			throw new PicoUnitException(e.getTargetException());
		}
	}
	
	private void invoke(String method, Object target, InvocationArgumentHandler argumentHandler) {
		try {
			invoker.invoke(method, target, argumentHandler);
		}
		catch (IllegalArgumentException e) {
			throw new PicoUnitException(e);
		}
		catch (IllegalAccessException e) {
			throw new PicoUnitException(e);
		}
		catch (InvocationTargetException e) {
			throw new PicoUnitException(e.getTargetException());
		}
	}
}
